package com.zyz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyz.dao.CoachDao;
import com.zyz.dao.CoachLessonDao;
import com.zyz.dao.UserLessonLineDao;
import com.zyz.dao.UsersDao;
import com.zyz.domain.*;
import com.zyz.handler.UserHolder;
import com.zyz.service.CoachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CoachServiceImpl implements CoachService {
    @Autowired
    private CoachDao coachDao;
    @Autowired
    private UsersDao usersDao;
    @Autowired
    private CoachLessonDao coachLessonDao;
    @Autowired
    private UserLessonLineDao userLessonLineDao;
    @Override
    public List<Coach> findCoach() {
        Integer id = UserHolder.get().getId();
        QueryWrapper<UserLessonLine> userLessonLineQueryWrapper = new QueryWrapper<>();
        userLessonLineQueryWrapper.lambda().eq(UserLessonLine::getMember_id,id);
        UserLessonLine one = userLessonLineDao.selectOne(userLessonLineQueryWrapper);
        Integer lesson_id = one.getLesson_id();

        QueryWrapper<CoachLesson> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(CoachLesson::getLesson_id,lesson_id);
        List<CoachLesson> lessonList = coachLessonDao.selectList(wrapper);
        List<Coach> arrayList = new ArrayList<>();
        QueryWrapper<User> wrapper1 = new QueryWrapper<>();
        wrapper1.lambda().eq(User::getId,id);
        User user = usersDao.selectOne(wrapper1);
        Integer coach_id1 = user.getCoach_id();
        String choose=null;
        if (coach_id1==null){
            choose="未选择";
        }else {
            choose="已被选择";
        }
        for (CoachLesson lesson : lessonList) {
            Integer coach_id = lesson.getCoach_id();
            QueryWrapper<Coach> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(Coach::getId,coach_id);
            Coach coach = coachDao.selectOne(queryWrapper);
            if (coach.getId()==coach_id1){
                coach.setChoose(choose);
            }else {
                coach.setChoose("未选择");
            }
            arrayList.add(coach);
        }
        return arrayList;
    }

    @Override
    public Integer getCoachTotal() {
       int i=0;
        List<Coach> coachList = coachDao.selectList(null);
        for (Coach coach : coachList) {
            i++;
        }
        return i;
    }
}
